
import java.util.ArrayList;

import com.intersys.globals.NodeReference;
import com.intersys.globals.ValueList;

public class InteractionGlobalsBD {

	private NodeReference global;
	private String name;
	private ValueList valueList;
	private Connection connection;

	InteractionGlobalsBD(String n, Connection conn) {
		connection = conn;
		name = n;
		global = connection.createNodeReference(name);
		valueList = connection.createList();
	}

	public boolean isEmpty() {
		return (global.exists());
	}

	public void store(int key, ArrayList<String> value) {
		global.appendSubscript(key);
		write(value, valueList);
		global.set(valueList);
		global.setSubscriptCount(0);
		valueList.clear();
		value.clear();
	}

	public boolean containsKey(String key) {
		global.appendSubscript(key);
		return (global.exists());
	}

	public void remove(String key) {
		global.appendSubscript(key);
		global.killNode();
	}

	public void clear() {
		global.kill();
	}

	public ArrayList<String> load(int key) {
		global.setSubscriptCount(0);
		global.appendSubscript(key);
		valueList = global.getList();
		ArrayList<String> date = new ArrayList<String>();
		read(date, valueList);
		return date;
	}

	public ArrayList<String> load(int key, ArrayList<String> data) {
		if (data == null) {
			throw new RuntimeException(
					"Cannot reconstruct object, no schema supplied");
		}
		valueList.clear();
		global.setSubscriptCount(0);
		global.appendSubscript(key);
		valueList = global.getList(valueList);
		read(data, valueList);
		return data;
	}

	private void write(ArrayList<String> dataList, ValueList valueList) {
		for (int i = 0; i < dataList.size(); i++) {
		valueList.append(dataList.get(i));
		}
	}

	private void read(ArrayList<String> dateList, ValueList valueList) {
		for (int i = 0; i < valueList.length(); i++) {
			dateList.add(valueList.getNextString());

		}
	}
}